{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib\n",
    "from matplotlib import pyplot as plt\n",
    "from sklearn import datasets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "digits = datasets.load_digits()   #手写数字数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['data', 'target', 'target_names', 'images', 'DESCR'])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "digits.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "#print(digits.DESCR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1797, 64)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = digits.data\n",
    "X.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1797,)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = digits.target\n",
    "y.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "digits.target_names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import sys\n",
    "# sys.path.append(\"G:/postgraduate_learning/machine learning algorithm/code/4-3 训练数据集，测试数据集\")\n",
    "from playML.model_selection import train_test_split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_ratio=0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "from playML.kNN_new import KNNClassifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_kNN_clf = KNNClassifier(k=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "KNN(k=3)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_kNN_clf.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_predict = my_kNN_clf.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 3, 1, 1, 8, 1, 0, 0, 1, 2, 4, 1, 2, 0, 8, 5, 6, 5, 0, 3, 3, 8,\n",
       "       8, 8, 8, 5, 3, 4, 4, 0, 8, 3, 4, 4, 4, 3, 0, 6, 8, 1, 5, 8, 7, 7,\n",
       "       1, 5, 0, 1, 7, 7, 2, 9, 4, 0, 8, 1, 1, 1, 4, 2, 2, 6, 5, 1, 1, 2,\n",
       "       9, 0, 2, 4, 9, 1, 8, 6, 4, 8, 4, 9, 4, 0, 4, 0, 3, 6, 4, 9, 7, 6,\n",
       "       2, 1, 8, 2, 3, 9, 7, 2, 9, 5, 8, 2, 1, 2, 6, 0, 2, 5, 9, 6, 8, 0,\n",
       "       9, 9, 5, 4, 7, 3, 7, 1, 0, 9, 0, 3, 8, 3, 1, 3, 4, 9, 5, 8, 7, 9,\n",
       "       9, 4, 7, 6, 8, 2, 4, 3, 2, 3, 2, 1, 9, 0, 7, 7, 5, 3, 5, 2, 1, 3,\n",
       "       4, 8, 7, 6, 4, 2, 4, 3, 3, 3, 6, 1, 6, 9, 3, 4, 2, 7, 6, 7, 6, 9,\n",
       "       1, 0, 5, 6, 2, 2, 9, 8, 2, 2, 5, 8, 5, 2, 2, 0, 9, 4, 3, 6, 5, 2,\n",
       "       9, 8, 3, 3, 8, 3, 3, 9, 8, 0, 6, 8, 3, 8, 9, 1, 1, 8, 4, 9, 1, 7,\n",
       "       4, 7, 3, 0, 6, 5, 8, 6, 3, 3, 5, 5, 9, 5, 3, 8, 5, 8, 7, 3, 1, 3,\n",
       "       7, 4, 1, 3, 3, 3, 2, 2, 0, 7, 3, 7, 8, 2, 5, 0, 2, 9, 1, 6, 5, 1,\n",
       "       5, 9, 3, 5, 1, 8, 1, 0, 4, 4, 2, 5, 6, 7, 9, 6, 2, 5, 7, 6, 3, 3,\n",
       "       4, 6, 8, 4, 7, 0, 5, 1, 9, 7, 7, 4, 2, 5, 4, 4, 9, 8, 3, 3, 9, 7,\n",
       "       4, 4, 2, 3, 8, 8, 3, 7, 7, 3, 8, 9, 2, 0, 2, 2, 8, 2, 9, 3, 9, 3,\n",
       "       3, 9, 7, 7, 0, 0, 2, 2, 7, 8, 7, 5, 2, 0, 5, 1, 6, 0, 1, 5, 4, 9,\n",
       "       5, 5, 6, 2, 8, 7, 2])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_predict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 3, 1, 1, 8, 1, 0, 0, 1, 2, 4, 1, 2, 0, 8, 5, 6, 5, 0, 3, 3, 8,\n",
       "       8, 8, 8, 5, 3, 4, 4, 0, 8, 3, 4, 4, 4, 3, 0, 6, 8, 1, 5, 8, 7, 7,\n",
       "       1, 5, 0, 1, 7, 7, 2, 9, 4, 0, 8, 1, 1, 1, 4, 2, 2, 6, 5, 1, 1, 2,\n",
       "       9, 0, 2, 4, 9, 1, 8, 6, 4, 8, 4, 9, 4, 0, 4, 0, 3, 6, 4, 9, 7, 6,\n",
       "       2, 1, 8, 2, 3, 9, 7, 2, 9, 5, 8, 2, 1, 2, 6, 0, 2, 5, 9, 6, 8, 0,\n",
       "       9, 9, 5, 4, 7, 3, 7, 1, 0, 9, 0, 3, 8, 9, 1, 3, 4, 9, 5, 8, 7, 9,\n",
       "       7, 4, 7, 6, 8, 2, 4, 3, 2, 3, 2, 1, 9, 0, 7, 7, 5, 3, 5, 2, 1, 3,\n",
       "       4, 8, 7, 6, 4, 2, 4, 3, 3, 3, 6, 1, 6, 9, 3, 4, 2, 7, 6, 7, 6, 9,\n",
       "       1, 0, 5, 6, 2, 2, 9, 8, 2, 2, 5, 8, 5, 2, 2, 0, 9, 4, 3, 6, 5, 2,\n",
       "       9, 8, 3, 3, 8, 3, 3, 9, 8, 0, 6, 8, 3, 8, 9, 1, 1, 8, 4, 9, 1, 7,\n",
       "       4, 7, 3, 0, 6, 5, 8, 6, 3, 3, 5, 5, 9, 5, 3, 8, 5, 8, 7, 3, 1, 3,\n",
       "       7, 4, 1, 3, 3, 3, 2, 2, 0, 7, 3, 7, 8, 2, 5, 0, 2, 9, 1, 6, 5, 1,\n",
       "       5, 9, 3, 5, 1, 8, 1, 0, 4, 4, 2, 5, 6, 7, 9, 6, 2, 5, 7, 6, 3, 3,\n",
       "       4, 6, 8, 4, 7, 0, 5, 1, 9, 7, 7, 4, 2, 5, 4, 4, 9, 8, 3, 3, 9, 7,\n",
       "       4, 4, 2, 3, 8, 8, 3, 7, 7, 3, 8, 9, 2, 0, 2, 2, 8, 2, 9, 3, 9, 2,\n",
       "       3, 9, 7, 7, 0, 0, 2, 2, 7, 8, 7, 5, 2, 0, 5, 1, 6, 0, 1, 5, 4, 9,\n",
       "       5, 5, 6, 2, 8, 7, 2])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9916434540389972"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum(y_predict == y_test)/len(y_test)        #预测准确度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "from playML.metrics import accuracy_score   #度量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9916434540389972"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "accuracy_score(y_test, y_predict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9916434540389972"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_kNN_clf.score(X_test, y_test)      #accuracy_score 进一步封装进kNN"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## sklearn中的accuracy_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "knn_clf = KNeighborsClassifier(n_neighbors=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',\n",
       "                     metric_params=None, n_jobs=None, n_neighbors=3, p=2,\n",
       "                     weights='uniform')"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "knn_clf.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_predict = knn_clf.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9888888888888889"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.metrics import accuracy_score\n",
    "accuracy_score(y_test, y_predict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
